﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/ExperienceDetail")]
    public class ExperienceDetailController : ApiController
    {
        B_ExperienceDetail _bed;
        public ExperienceDetailController()
        {
            _bed = new B_ExperienceDetail();
        }

        /// <summary>
        /// 获取所有经历：分页
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll(int page,int size)
        {
            var data = from ed in _bed.GetAll()
                       join et in new B_ExperienceType().GetAll() on ed.ETypeID equals et.ETypeID
                       select new Param_ExperienceDetail
                       {
                           ETypeID = ed.ETypeID,
                           EDID = ed.EDID,
                           EDOne = ed.EDOne,
                           EDTwo = ed.EDTwo,
                           EDThressTime = ed.EDThressTime,
                           EDThressOutTime = ed.EDThressOutTime,
                           EDFour = ed.EDFour,
                           EDFiveDescribe = ed.EDFiveDescribe,
                           IsOnTrue = ed.IsOnTrue,
                           TemplateID=ed.TemplateID,
                           EName=et.EName
                       };
            return Ok(new Result<List<Param_ExperienceDetail>>()
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg=data.Count().ToString()
            });
        }

        /// <summary>
        /// 获取所有经历
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetAll()
        {
            var data = from ed in _bed.GetAll()
                       select new Param_ExperienceDetail
                       {
                           ETypeID = ed.ETypeID,
                           EDID = ed.EDID,
                           EDOne = ed.EDOne,
                           EDTwo = ed.EDTwo,
                           EDThressTime = ed.EDThressTime,
                           EDThressOutTime = ed.EDThressOutTime,
                           EDFour = ed.EDFour,
                           EDFiveDescribe = ed.EDFiveDescribe,
                           IsOnTrue = ed.IsOnTrue,
                           TemplateID = ed.TemplateID
                       };
            return Ok(new Result<List<Param_ExperienceDetail>>()
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 查询指定模板下的各个分类下的经历详情
        /// </summary>
        /// <returns></returns>
        [Route("GetDataByTemplateID/{id}")]
        [HttpGet]
        public IHttpActionResult GetDataByTemplate(int id)
        {
            //var details = _bed.GetAll();
            //var type = new B_ExperienceType().GetAll();
            //var data = from ed in details
            //           join t in type on ed.ETypeID equals t.ETypeID
            //           where ed.TemplateID == id
            //           orderby t.EPower ascending
            //           group ed by t into g
            //           select new Param_Experience
            //           {
            //               ETypeID = g.Key.ETypeID,
            //               EName = g.Key.EName,
            //               EPower = g.Key.EPower,
            //               IsShow = g.Key.IsShow,
            //               ListExperienceDetail = (from d in g
            //                                       select new Param_ExperienceDetail
            //                                       {
            //                                           EDID = d.EDID,
            //                                           EDOne = d.EDOne,
            //                                           EDTwo = d.EDTwo,
            //                                           EDThressTime = d.EDThressTime,
            //                                           EDThressOutTime = d.EDThressOutTime,
            //                                           EDFour = d.EDFour,
            //                                           EDFiveDescribe = d.EDFiveDescribe,
            //                                           ETypeID = d.ETypeID,
            //                                           IsOnTrue = d.IsOnTrue,
            //                                           TemplateID = d.TemplateID
            //                                       }).ToList()


            //           };
            //return Ok(new Result<List<Param_Experience>>()
            //{
            //    Data = data.ToList()
            //});
            return Ok(new Result<List<IDAL.Param_Experience>>()
            {
                Data = _bed.GetDataByTemplate(id)
            });
        }

        /// <summary>
        /// 创建经历
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(Param_ExperienceDetail param)
        {
            try
            {
                _bed.Add(new ExperienceDetail {
                    EDOne = param.EDOne,
                    EDTwo = param.EDTwo,
                    EDThressTime = param.EDThressTime,
                    EDThressOutTime = param.EDThressOutTime,
                    EDFour = param.EDFour,
                    EDFiveDescribe = param.EDFiveDescribe,
                    ETypeID = param.ETypeID,
                    IsOnTrue = param.IsOnTrue,
                    TemplateID = param.TemplateID,
                });
                return Ok(new Result<object>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }

        }

        /// <summary>
        /// 删除经历
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpGet]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                if (_bed.FindById(id)==null)
                {
                    return Ok(new Result<object>()
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                _bed.Delete(id);
                return Ok(new Result<object>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }

        }

        /// <summary>
        /// 查询经历详情
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Find/{id}")]
        [HttpGet]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = _bed.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空s"
                    });
                }
                return Ok(new Result<Param_ExperienceDetail>
                {
                    Data = new Param_ExperienceDetail
                    {
                        ETypeID = data.ETypeID,
                        EDID = data.EDID,
                        EDOne = data.EDOne,
                        EDTwo = data.EDTwo,
                        EDThressTime = data.EDThressTime,
                        EDThressOutTime = data.EDThressOutTime,
                        EDFour = data.EDFour,
                        EDFiveDescribe = data.EDFiveDescribe,
                        IsOnTrue = data.IsOnTrue,
                        TemplateID = data.TemplateID,
                        EName=new B_ExperienceType().FindById(data.ETypeID.Value).EName
                    }
                });
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }



        /// <summary>
        /// 修改经历
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update/{id}")]
        [HttpGet]
        public IHttpActionResult Update(int id, Param_ExperienceDetail param)
        {
            try
            {
                var data = _bed.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                _bed.Update(id, new ExperienceDetail
                {
                    EDOne = param.EDOne,
                    EDTwo = param.EDTwo,
                    EDThressTime = param.EDThressTime,
                    EDThressOutTime = param.EDThressOutTime,
                    EDFour = param.EDFour,
                    EDFiveDescribe = param.EDFiveDescribe,
                    ETypeID = param.ETypeID,
                    IsOnTrue = param.IsOnTrue,
                    TemplateID = data.TemplateID,
                });
                return Ok(new Result<ExperienceDetail>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }
    }
}
